/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.base.web;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.mapper.JsonMapper;
import com.chlitina.store.common.utils.StoreProductUtils;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.base.entity.StoreCare;
import com.chlitina.store.modules.base.entity.StoreProduct;
import com.chlitina.store.modules.base.entity.StoreSelfProduct;
import com.chlitina.store.modules.base.service.StoreProductService;
import com.chlitina.store.modules.expert.entity.Product;
import com.chlitina.store.modules.expert.entity.ProductCategoryStoreType;
import com.chlitina.store.modules.sys.utils.StoreUtils;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

/**
 * 店面产品Controller
 * 
 * @author liujiang
 * @version 2013-09-26
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH + "/base/storeProduct")
public class StoreProductController extends BaseController {

	@Autowired
	private StoreProductService storeProductService;

	@ModelAttribute
	public StoreProduct get(@RequestParam(required = false) Long id) {
		if (id != null) {
			return storeProductService.get(id);
		} else {
			return new StoreProduct();
		}
	}

	@ResponseBody
	@RequestMapping(value = "storeProductData")
	public String storeProductData(@RequestParam(required=false) int type, Integer productType, HttpServletResponse response) {
		response.setContentType("application/json; charset=UTF-8");
		Set<Map<String, Object>> set = Sets.newHashSet();
		String storeTypeId = StoreUtils.getStore().getStoreType().getId().toString();
		if (productType == null) {
			productType = -1;
		}
		List<StoreProduct> productList = storeProductService.findByStatus(type, productType);
		for (StoreProduct storeProduct : productList) {
			if (storeProduct == null) {
				continue;
			}
			Map<String, Object> map = Maps.newHashMap();
			
			if(StoreCare.TYPE_SYS == type) {
				if(storeProduct.getProduct().getStatus() == Product.FLAG_DISABLE){
					continue;
				}
				String storeTypeIds = "";
				for(ProductCategoryStoreType cst : storeProduct.getProduct().getProductCategory().getStoreTypeList()){
					storeTypeIds += cst.getStoreType().getId() + ",";
				}
				if(!storeTypeIds.contains(storeTypeId)){
					continue;
				}
			} else {
				if(storeProduct.getStoreSelfProduct().getStatus() == StoreSelfProduct.FLAG_DISABLE){
					continue;
				}
			}
			map.put("id", storeProduct.getId());
			map.put("name", StoreProductUtils.getName(storeProduct));
			map.put("retailPrice", StoreProductUtils.getRetailPrice(storeProduct));
			map.put("spec", StoreProductUtils.getSpec(storeProduct));
			map.put("category", StoreProductUtils.getCategory(storeProduct));
			set.add(map);
		}
		//
		System.err.println(JsonMapper.getInstance().toJson(set));
		return JsonMapper.getInstance().toJson(set);
	}
}
